package com.igeek.boot.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeek.boot.dto.DeptEventStatisticDTO;
import com.igeek.boot.dto.EventStatisticDTO;
import com.igeek.boot.dto.MonthDateCountDTO;
import com.igeek.boot.entity.ZhrjAlarmInfo;
import com.igeek.boot.service.ZhrjAlarmInfoService;
import com.igeek.boot.common.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @package com.igeek.boot.controller
 * @Description 事件信息controller层
 * @Author LittlePunk [296526219@qq.ccom]
 * @Version 2022.1
 * @Date 2023/12/12 19:51
 */
@RestController
@RequestMapping("/zhrj/events")
@Api(tags = "事件信息表的接口")
public class EventStatisticController {

    @Autowired(required = false)
    private ZhrjAlarmInfoService zhrjAlarmInfoService;
    //按照类型进行统计查询
    @ApiOperation("事件按照类型统计查询")
    @GetMapping("/statistic")
    public Result<Map<String,Object>> findByType(){
        //封装查询条件  进行分组groupBy进行查询  然后统计总数
        List<EventStatisticDTO> eventStatisticDTOS = zhrjAlarmInfoService.findByType();
        //根据部门id进行分组查询
        List<DeptEventStatisticDTO> deptEventStatisticDTOS= zhrjAlarmInfoService.findByDeptId();
        Map<String, Object> map = new HashMap<>();
        map.put("根据eventType进行分组查询",eventStatisticDTOS);
        map.put("根据deptId进行分组查询",deptEventStatisticDTOS);
        return new Result<>("查询成功",200,map);
    }

    //按照不同部门的事件数量
    @GetMapping("/statistic/dept")
    public List<DeptEventStatisticDTO> findByDeptId(){
        //调用
        List<DeptEventStatisticDTO> dtos = zhrjAlarmInfoService.findByDeptId();
        return dtos;
    }

    //月度事件数量趋势
    @ApiOperation("月度时间趋势接口")
    @GetMapping("/month_date_count/{month}")
    public Result<List<MonthDateCountDTO>> findMonthDateCount(@PathVariable String month){
        //先对日期进行处理
        String[] yearMonth = month.split("-");
        int year = Integer.parseInt(yearMonth[0]);
        int monthIndex = Integer.parseInt(yearMonth[1]);
        LocalDate dateTime = LocalDate.of(year, monthIndex,1);
        //下一天的日期
        LocalDate nextMonthTime = dateTime.plusMonths(1);
        List<MonthDateCountDTO> dtos = zhrjAlarmInfoService.findMonthDateCount(dateTime,nextMonthTime);
        return new Result<>("查询成功",200,dtos);
    }

    //对事件信息表进行分页查询  传入页码  传入查询的条数
    @ApiOperation("自定义分页查询")
    @PostMapping("page/{current}/{size}")
    public Result<Page<ZhrjAlarmInfo>> pageByZhrj(
            @PathVariable("current") Integer current,
            @PathVariable("size") Integer size,
            //查询条件
            @RequestBody ZhrjAlarmInfo query
    ){
        //查询信息
        Page<ZhrjAlarmInfo> page = zhrjAlarmInfoService.pageByZhrj(current,size,query);
        return new Result<>("查询成功",200,page);
    }

}
